
var app = require('../../server/server');
module.exports = function(Wifiinfo) {

	Wifiinfo.searchData = function (searchText, lat, lng, cb) {
		var now = new Date();
		now.setDate(now.getDate() - 3);
	  var option = {
	    where: {
	    	and: [
		      {or: [
		        {WIFIMAC: {like: '%' + searchText + '%'}},
		        {WIFINAME: {like: '%' + searchText + '%'}}
		      ]},
	    		{CJSJ: {gt: now}}
	    	]
	    }
	  };
	  var result = [];
	  Wifiinfo.find(option, function (err, data) {
	    for(var i = 0; i < data.length; i++) {
	      result.push({
	        type: 'mac',
	        msg: data[i].WIFIMAC,
	        addr: data[i].WIFINAME,
	        lat: data[i].WEIDU - 0,
	        lng: data[i].JINGDU - 0,
	        time: data[i].CJSJ,
	        distance: Math.floor(Math.abs(data[i].WEIDU - lat) * 110000 
	          + Math.abs(data[i].JINGDU - lng) * 94000)
	      })
	    }
	    cb(null, result);
	  })
	};

	Wifiinfo.remoteMethod(
	  'searchData', {
	    http: {path: '/searchData', verb: 'post'},
	    accepts: [
	      {arg: 'searchText', type: 'string', required: 'true'},
	      {arg: 'lat', type: 'number', required: 'true'},
	      {arg: 'lng', type: 'number', required: 'true'}
	    ],
	    returns: {type: 'array', root: true}
	  }
	);

};
